﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DCTAccess.Business;
using DCTLib.Common;
using System.Data;

namespace DCTAccess.Business.Administrator
{
    public class AdministratorController
    {
        #region [Control Action] ADD_NEW  /   UPDATE  /   DELETE  /   CHANGE_PASS /   UPDATE_LAST_LOGIN
        public static bool Insert(Administrator admin)
        {
            return ExecuteStoredProcedure(admin, StoreProceduAction.AddNew.ToString());
        }

        public static bool Update(Administrator admin)
        {
            return ExecuteStoredProcedure(admin, StoreProceduAction.Update.ToString());            
        }

        public static bool Delete(Administrator admin)
        {
            return ExecuteStoredProcedure(admin, StoreProceduAction.Delete.ToString());
        }

        public static bool ChangePassword(Administrator admin)
        {
            return ExecuteStoredProcedure(admin, "ChangePassword");
        }

        //public static int ChangePass(Administrator admin)
        //{
        //    SQLHelper sqlHelper = new SQLHelper();
        //    sqlHelper.SwitchCommandType(CommandType.Text);
        //    string sql = "UPDATE Web_Admins SET  HashedPassword = '"+admin.HashedPassword+"' WHERE AdminID = '"+admin.ID+"' ";
        //    return int.Parse(sqlHelper.ExecuteScalar(sql).ToString());
        //}

        public static bool UpdateLastLoginDate(Administrator admin)
        {
            return ExecuteStoredProcedure(admin, "UpdateLastLoginDate");
        }
        #endregion

        public static AdministratorCollection GetAdministrators(AdministratorFilter searchCriteria)
        {
            DataTable adminDatatable = GetAdminstratorsByFilter(searchCriteria);

            if (ValidationHelper.HasRow(adminDatatable))
            {
                return new AdministratorCollection(adminDatatable);
            }
            else
            {
                return null;
            }
        }

        public static Administrator GetAdministratorByID(int administratorID)
        {
            AdministratorFilter searchCriteria = new AdministratorFilter();
            searchCriteria.ID = administratorID;

            DataTable dtAdmin = searchCriteria.GetAdministrators();
            if (ValidationHelper.HasRow(dtAdmin))
            {
                return new Administrator(dtAdmin.Rows[0]);
            }
            else
            {
                return null;
            }
        }
                

        public static Administrator GetAdministratorByUsername(string username)
        {
            if (string.IsNullOrEmpty(username))
            {
                return null;
            }

            AdministratorFilter adminFilter = new AdministratorFilter();
            adminFilter.UserName = username;

            DataTable dtAdmin = adminFilter.GetAdministrators();
            if (ValidationHelper.HasRow(dtAdmin))
            {
                return new Administrator(dtAdmin.Rows[0]);
            }
            else
            {
                return null;
            }
        }

        public static DataTable GetAdminstratorsByFilter(AdministratorFilter searchCriteria)
        {
            return searchCriteria.GetAdministrators();
        }

        private static bool ExecuteStoredProcedure(Administrator admin, string action)
        {
            SQLHelper sqlHelper = new SQLHelper();
            string sProc = "Administrator_spr";
            sqlHelper.AddParameter("@Action", action);
            sqlHelper.AddParameter("@AdminID", admin.ID);
            sqlHelper.AddParameter("@AdminLoginName", admin.UserName);
            sqlHelper.AddParameter("@HashedPassword", admin.HashedPassword);
            sqlHelper.AddParameter("@FullName", admin.FullName);
            sqlHelper.AddParameter("@Role", admin.Role);
            sqlHelper.AddParameter("@Mobile", admin.Mobile);
            sqlHelper.AddParameter("@Address", admin.Address);
            sqlHelper.AddParameter("@Email", admin.Email);
            return sqlHelper.ExecuteSQL(sProc);
        }
    }
}
